#include<iostream>
#include<string.h>
#include<memory.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

typedef struct LNode
{
	int data;
	struct LNode *next;
}tLNode;

tLNode * head;

void addNode(int n);

void createLink(int array[],int len)
{
	head = new tLNode();//head = (tLNode*)malloc(sizeof(tLNode));
	head->next = NULL;
	for(int i=0;i<len;i++)
	{
		addNode(array[i]);
	}
}

void 
addNode(int n)
{
	tLNode *p,*q;
	p = new tLNode();//p = (tLNode *)malloc(sizeof(tLNode));
	q = head;
	while(q->next != NULL )
		q = q->next;
	q->next = p;
	p->data = n;
	p->next = NULL;
}

void 
findTheLastNth(int n)
{
	tLNode *p,*q;
	p = head;
	for(int i=0;i<n;i++)
		p=p->next;
	
	q = head->next;
	while(p->next!=NULL)
	{
		q = q->next;
		p = p->next;
	}
	printf("The last %dth number is: %d\n",n,q->data);
}

void 
display()
{
	tLNode *q;
	q = head;
	while(q->next!=NULL)
	{
		q = q->next;
		cout<<q->data<<" ";
	}
	cout<<endl;
}

int 
main()
{
	int a[]={3,2,1,6,5,4,9,8,7,10,2,1,5,6,4};
	int len=sizeof(a)/sizeof(a[0]);
	int n ;
	createLink(a,len);
	display();
	cout<<"please input the n which means the last nth number of the array :"<<endl;
	cin>>n;
	findTheLastNth(n);
	return 0;
}
